﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sponsorship.Domain;
using Raven.Client;
using Sponsorship.Domain.Presents;
using Sponsorship.ReadModel.Persons;
using Sponsorship.ReadModel.Sponsors;

namespace Sponsorship.ReadModel.Presents
{
    public class ListOfPresentsHandlers :
        IEventHandler<PresentEntered>,
        IEventHandler<PresentChanged>
    {
        private readonly IDocumentStore m_documentStore;

        public ListOfPresentsHandlers(IDocumentStore docStore)
        {
            m_documentStore = docStore;
        }


        public void Handle(PresentEntered domainEvent)
        {
            using (var session = m_documentStore.OpenSession())
            {
                var person = session.LoadById<ListOfPersonDto>(domainEvent.PersonId);
                var sponsor = session.LoadById<SponsorListItemDto>(domainEvent.SponsorId);

                var dto = new ListOfPresentsDto
                {
                    AggregateRootId = domainEvent.PresentId,

                    Comment = domainEvent.Comment,
                    Amount = domainEvent.PresentData.Amount,
                    Description = domainEvent.PresentData.Description,

                    PersonId = domainEvent.PersonId,
                    PersonName = GeneratePersonName(person.FirstName , person.LastName),

                    SponsorId = domainEvent.SponsorId,
                    SponsorName =GenerateSponsorName( sponsor.FirstName ,person.LastName),

                    PresentTemplateId = domainEvent.PresentTemplateId
                };

                session.SaveChanges(dto, domainEvent);
            }
        }

        public void Handle(PresentChanged domainEvent)
        {
            using (var session = m_documentStore.OpenSession())
            {
                var present = session.LoadById<ListOfPresentsDto>(domainEvent.PresentId);

                var person = session.LoadById<ListOfPersonDto>(domainEvent.PersonId);
                var sponsor = session.LoadById<SponsorListItemDto>(domainEvent.SponsorId);

                present.Comment = domainEvent.Comment;
                present.Amount = domainEvent.PresentData.Amount;
                present.Description = domainEvent.PresentData.Description;

                present.PersonId = domainEvent.PersonId;
                present.PersonName = GeneratePersonName(person.FirstName, person.LastName);

                present.SponsorId = domainEvent.SponsorId;
                present.SponsorName = GenerateSponsorName(sponsor.FirstName, sponsor.LastName);

                session.SaveChanges(present, domainEvent);
            }
        }



        private string GeneratePersonName(string first, string last)
        {
            return first + " " + last;
        }
        private string GenerateSponsorName(string first, string last)
        {
            return first + " " + last;
        }
    }
}
